var API = require('API').survey.order;
var DateUtil = require('DateUtil');
var cols = [
    {
        field: 'name',
        headerName: '问卷名',
        filterParams: {newRowsAction: 'keep'}
    },
    {
        field: 'user_name',
        headerName: '客服',
        filterParams: {newRowsAction: 'keep'}
    },
    {
        field: 'customer_name',
        headerName: '客户姓名',
        filterParams: {newRowsAction: 'keep'}
    },
    {
        field: 'customer_phone',
        headerName: '客户电话',
        filterParams: {newRowsAction: 'keep'}
    },
    {
        field: 'create_time',
        headerName: '创建时间',
        filterParams: {newRowsAction: 'keep'},
        cellRenderer: function (p) {
            if (p.value) return DateUtil.format(p.value);
            return "";
        }
    },

    {
        headerName: '操作',
        cellRenderer: function (p) {
            var container = document.createElement('span');

            var open = document.createElement('a');
            open.innerHTML = '<i class="fa fa-folder-open" style="padding:0 .25em" title="打开"></i>';
            open.addEventListener('click', function () {
                callbacks.open(p.data)
            });

            //var del = document.createElement('a');
            //del.innerHTML = '<i class="fa fa-trash-o" style="padding:0 .25em;color:darkred" title="删除"></i>';
            //del.addEventListener('click', function () {
            //    if (confirm("确定要删除？")) {
            //        callbacks.del(p.data);
            //    }
            //});

            container.appendChild(open);
            //container.appendChild(del);

            return container;
        },
        suppressMenu: true,
        suppressSorting: true
    }
];

var getData = function () {

    return {
        getRows: function (p) {
            var qo = {};

            //pagination
            qo.offset = p.startRow;
            qo.limit = p.endRow - p.startRow;

            //sort
            if (p.sortModel.length > 0) {
                qo.sort = '';
                p.sortModel.forEach(function (val) {
                    qo.sort += (val.sort == 'desc' ? '-' : '+') + val.colId + ", ";
                });
            }
            //filter
            var search = p.filterModel;

            if (search) {
                Object.keys(search).forEach(function (val) {
                    var searchObj = search[val];
                    switch (searchObj.type) {
                        case 'set':
                            qo[val] = "in," + searchObj.values.join(',');
                            break;
                        case 1:
                            qo[val] = "lk,%" + searchObj.filter + "%";
                            break;
                        case 2:
                            qo[val] = searchObj.filter;
                            break;
                    }
                });
            }
            API.search(qo, function (json, status, xhr) {
                var count = xhr.getResponseHeader('X-Total-Count');
                p.successCallback(json, Number(count));
            });
        },
        pageSize: 50

    }
};
var callbacks = {
    "open": function () {
        alert('open')
    },
    "del": function () {
        alert('del')
    }
    //"statusChanged":function(){alert('statusChanged')}
};

var t = {
    grid: {
        //angularCompileRows     : true,
        columnDefs: cols,
        rowData: [],
        datasource: getData(),
        rowModelType: 'pagination',
        enableServerSideSorting: true,
        enableServerSideFilter: true,
        suppressCellSelection: true,
        rowHeight: 22,
        colWidth: 100,
        enableColResize: true,
        enableCellExpressions: true,
        api: {},
        columnApi: {},
        rowSelection: 'single',
        localeText: require('locale').aggrid
    },
    refresh: function () {
        t.grid.api.setDatasource(getData());
    },
    on: function (name, cb) {
        callbacks[name] = cb;
    }
};

module.exports = t;
